<?php
/**
 * Controller   系统核心
 * @copyright   Copyright(c) 2024-2034
 * @author      tianjie <527707665@qq.com>
 * @link        http://www.tinphp.com/
 * @version     4.1
 */

namespace tian;

use think\Template as ThinkTemplate;

class Controller
{
    private $default;
    private $options;
    private $viewPath;
    private $cachePath;

    private $temp;

    protected $_vars = [];

    /**
     * action前执行的全局方法，可继承并重构
     */
    public function _before_action()
    {

    }

    /**
     * action后执行的全局方法,可继承并重构
     */
    public function _after_action()
    {

    }

    /**
     * 魔术方法
     */
    public function __call($name, $arguments)
    {
        exit("error url 404");
    }


    /**
     * 系统初始化函数，主要初始化模板引擎，定义一些主要的全局模板路径
     * @param string $m 模块名称
     * @param string $c 控制器名称
     */
    public function _sys_init_action(string $m, string $c)
    {
        $app = new App();
        $ViewPath = $app->getViewPath();
        $RuntimePath = $app->getRuntimePath();

        $viewConfig = config('view');
        $this->default = $viewConfig['default'];
        $this->options = $viewConfig['options'][$this->default];
        $this->viewPath = $ViewPath . $m . "/" . strtolower(toUnderLineName($c)) . '/';
        $this->cachePath = $RuntimePath . '/views/';

        if ($viewConfig['is_open']) {
            if (strtoupper($this->default) == strtoupper('ThinkTemplate')) {
                //设置模板引擎参数
                $configTemp = [
                    //模板路径
                    'view_path' => $this->viewPath,
                    //模板缓存目录
                    'cache_path' => $this->cachePath,
                ];
                $options = array_merge($configTemp, $this->options);

                if (class_exists("\\think\\Template")) {
                    $this->temp = new ThinkTemplate();
                    $this->temp->config($options);
                } else {
                    exit('请先 composer require topthink/think-template 安装视图模板');
                }
            } else {
                //其他模板引擎
                exit("暂时只支持 ThinkTemplate 模板引擎。");
            }
        }
    }

    /**
     * 模板参数绑定
     * @param $name
     * @param null $value
     */
    public function assign($name, $value = null)
    {
        $this->_vars = \array_merge($this->_vars, \is_array($name) ? $name : [$name => $value]);
    }

    /**
     * 获取模板解析后的内容
     * @param string $tplName
     * @param array $value
     * @return mixed
     */
    public function fetch(string $tplName, array $value = [])
    {
        $app = new App();
        $ViewPath = $app->getViewPath();

        $view_suffix = $this->options['view_suffix'] != '' ? $this->options['view_suffix'] : 'html';
        $temp = $ViewPath . fileRoutePatch($tplName) . '.' . $view_suffix;
        if (file_exists($temp)) {
            $value = \array_merge($this->_vars, $value);
            if($this->default == 'ThinkTemplate'){
                return $this->temp->fetch($tplName, $value);
            }
            exit($this->default . ' is currently not supported');
        } else {
            header('Content-Type:application/json; charset=utf-8');
            exit("404 The page not found：" . $temp);
        }
    }

    /**
     * 系统自带跳转提示 success
     * @param string $msg
     * @param string $url
     * @return mixed
     */
    public function success(string $msg, string $url = '/')
    {
        $RootPath = realpath(dirname(__DIR__));

        $data['code'] = 1;
        $data['msg'] = $msg;
        $data['url'] = url($url);
        $data['wait'] = 3;
        return $this->temp->fetch($RootPath . '/tian/temp/toast.html', $data);
    }

    /**
     * 系统自带跳转提示 error
     * @param string $msg
     * @param string $url
     * @return mixed
     */
    public function error(string $msg, string $url = '')
    {
        $RootPath = realpath(dirname(__DIR__));

        $data['code'] = 0;
        $data['msg'] = $msg;
        $data['url'] = url($url);
        $data['wait'] = 3;
        return $this->temp->fetch($RootPath . '/tian/temp/toast.html', $data);
    }

}
